package com.alfayun.fiscalriskanalysis.reportspec;

import java.util.List;
import java.util.Optional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Order;
import org.springframework.stereotype.Service;

@Service
public class RowSpecService {

	@Autowired
	private RowSpecRepository repository;
	
	public List<RowSpec> list() {
		return repository.findAll(Sort.by(Order.desc("id")));
	}
	
	public RowSpec queryById(int id) throws NoSuchColumnSpecException {
		return repository.findById(id).orElseThrow(NoSuchColumnSpecException::new);
	}
	
	public RowSpec newRowSpec(RowSpec rowSpec) {
		return RowSpec.newRowSpec(repository, rowSpec);
	}

	public void deleteRowSpec(int id) {
		repository.deleteById(id);
	}

	public RowSpec modifyRowSpec(int id, RowSpec rowSpec) throws NoSuchColumnSpecException {
		Optional<RowSpec> row = repository.findById(id);

		if (row.isPresent()) {
			return repository.save(rowSpec);
		}

		throw new NoSuchColumnSpecException();
	}
	
}
